package com.jsyl.lwbk.web.controller.education;

import com.github.pagehelper.Page;
import com.jsyl.lwbk.business.domain.education.CourseChild;
import com.jsyl.lwbk.business.domain.education.TrainingProgramChildRecord;
import com.jsyl.lwbk.business.dto.education.CourseListDTO;
import com.jsyl.lwbk.business.dto.education.TrainingProgramChildRecordListDTO;
import com.jsyl.lwbk.business.service.education.IChildCourseService;
import com.jsyl.lwbk.business.service.education.ITrainingProgramChildRecordService;
import com.jsyl.lwbk.business.vo.education.ChildCourseVO;
import com.jsyl.lwbk.common.constant.Constants;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.common.exception.BusinessException;
import com.jsyl.lwbk.common.utils.StringUtils;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import com.jsyl.lwbk.system.domain.SysRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/education/childScheduleRecord")
public class ChildScheduleRecordController extends BaseController {

    private String prefix = "education/childScheduleRecord";

    @Autowired
    private IChildCourseService childCourseService;

    @Autowired
    private ITrainingProgramChildRecordService iTrainingProgramChildRecordService;

    @RequiresPermissions("education:childScheduleRecord:view")
    @GetMapping()
    public String childSchedule(ModelMap mmap) {
        mmap.put("teacherId", ShiroUtils.getUserId());
        return prefix + "/childScheduleRecord";

    }

    @GetMapping("/studentEntering/{childId}/{projectType}/{trainingProgramNumber}/{centerId}")
//    @RequiresPermissions("education:studentEntering:view")
    public String studentEntering(@PathVariable("childId") Long childId,
                                  @PathVariable("projectType") Long projectType,
                                  @PathVariable("trainingProgramNumber") String trainingProgramNumber,
                                  @PathVariable("centerId") Long centerId,ModelMap mmap) {
        System.out.println(childId);
//        ChildCourseVO childCourseVO = childCourseService.findById(id);
//        if(CollectionUtils.isEmpty(childCourseVO.getStudents())){
//            throw new BusinessException("该课程没有小孩");
//        }
//        if(!CollectionUtils.isEmpty(childCourseVO.getStudents())){
//            List<CourseChild> courseChildList = childCourseVO.getStudents();
//           for(int i = 0;i<courseChildList.size();i++){
//               courseChildList.get(i).setIndex(i);
//           }
//        }
        mmap.put("trainingProgramNumber", trainingProgramNumber);
//        mmap.put("students", childCourseVO.getStudents());
        mmap.put("projectType", projectType);
        mmap.put("centerId", centerId);
        mmap.put("childId", childId);
//        mmap.put("teacherId", childCourseVO.getTeacherId());
//        mmap.put("teacherName", childCourseVO.getTeacherName());
//        mmap.put("courseId", childCourseVO.getCourseId());
        return prefix + "/studentEntering";
    }

    /**
     * 保存成绩
     * @param records
     * @return
     */
    @PostMapping("/saveEntering")
    @ResponseBody
    public AjaxResult saveEntering(@RequestBody  List<TrainingProgramChildRecord> records) {
        iTrainingProgramChildRecordService.saveEntering(records, ShiroUtils.getUserId(),ShiroUtils.getLoginName());
        return toAjax(Boolean.TRUE);
    }


    @GetMapping("/studentEnteringShow/{id}/{isCurDay}")
    public String studentEnteringShow(@PathVariable("id") Long id,@PathVariable("isCurDay") Short isCurDay,ModelMap mmap) {
        ChildCourseVO childCourseVO = childCourseService.findById(id);
        if(!CollectionUtils.isEmpty(childCourseVO.getStudents())){
            List<CourseChild> courseChildList = childCourseVO.getStudents();
            for(int i = 0;i<courseChildList.size();i++){
                courseChildList.get(i).setIndex(i);
            }
        }
        mmap.put("childCourse", childCourseService.findById(id));
        mmap.put("scheduleTime", childCourseVO.getScheduleTime());
        mmap.put("students", childCourseVO.getStudents());
        mmap.put("projectType", childCourseVO.getProjectType());
        mmap.put("centerId", childCourseVO.getCenterId());
        mmap.put("curChildId", childCourseVO.getStudents().get(0).getChildId());
        mmap.put("courseId", id);
        mmap.put("isCurDay", isCurDay);
        return prefix + "/studentEnteringShow";

    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseListDTO courseListDTO) {
        boolean isTeacher = checkTeacher();
        if(!isTeacher){
            courseListDTO.setTeacherId(null);
        }
        Page page = startPage();
        List<ChildCourseVO> list = childCourseService.list(courseListDTO);
        return getDataTable(list,page);
    }

    private boolean checkTeacher(){
        List<SysRole> sysRoles = ShiroUtils.getSysUser().getRoles();
        if(CollectionUtils.isEmpty(sysRoles)){
            throw new BusinessException("无权限");
        }
        List<String> roleKeys = new ArrayList<String>();
        for(SysRole sysRole:sysRoles){
            roleKeys.add(sysRole.getRoleKey());
        }
        if(roleKeys.contains(Constants.ROLE_SUPERADMIN)){
            return false;
        }
//        if(!roleKeys.contains(Constants.ROLE_NORMALADMIN) && !roleKeys.contains(Constants.ROLE_TEACHERCHIEF) && !roleKeys.contains(Constants.ROLE_TEACHER) && !roleKeys.contains(Constants.ROLE_TEACHINGRESEARCH)){
//            throw new BusinessException("无权限");
//        }
        boolean isTeacher =false;
        if(roleKeys.contains(Constants.ROLE_TEACHER) &&roleKeys.size()==1){
            isTeacher = true;
        }
        return isTeacher;
    }

    @PostMapping("/listEnteringShow")
    @ResponseBody
    public AjaxResult listEnteringShow(TrainingProgramChildRecordListDTO trainingProgramChildRecordListDTO) {
        //当天查看前一天成绩不能带courseId
        if(trainingProgramChildRecordListDTO.getCheckTime() == 1){
            trainingProgramChildRecordListDTO.setCourseId(null);
        }
        String trainingTime = iTrainingProgramChildRecordService.getLastTrainingTime(trainingProgramChildRecordListDTO);
        if(StringUtils.isEmpty(trainingTime)){
            return AjaxResult.success(new ArrayList<TrainingProgramChildRecord>());
        }
        else {
            trainingProgramChildRecordListDTO.setTrainingTime(trainingTime);
        }
        List<TrainingProgramChildRecord> list = iTrainingProgramChildRecordService.list(trainingProgramChildRecordListDTO);
        return AjaxResult.success(list);
    }

}
